<?php
require_once("lib/autoload.php");

#header('Content-type: application/json');

extract($_GET);

if (!isset($actionId)) {
    print json_encode(array("success"=>false));
    return;
}
if (!isset($page)) {
    $page = 1;
}
if (!isset($pageSize)) {
    $pageSize = 10;
}


$dc = new DataController();
$dc->connect();    
$commentsPage = $dc->getComments($actionId, $pageSize, $page);
$dc->disconnect();

$commentsArray = array();

if ($commentsPage != NULL) {
    foreach($commentsPage->getComments() as $comment) {
        $commentsArray[] = array("user"=>$comment->getUser(),
                                 "comment"=>$comment->getComment(),
                                 "date"=>date("d-m-Y H:i:s", $comment->getTime()));
    }
    print json_encode(array("success"=>true,
                            "total"=>$commentsPage->getTotal(),
                            "page"=>$commentsPage->getPageIndex(),
                            "pageSize"=>$commentsPage->getPageSize(),
                            "comments"=>$commentsArray));
}
else {
    print json_encode(array("success"=>false));
}

?>